package org.skydiveapp;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;

import org.skydiveapplibrary.data.Location;
import org.skydiveapplibrary.repository.LocationRepository;

public class LocationEditActivity extends Activity
{
	// requests
	private static final int REQUEST_NOTES = 0;

	// repository
	private LocationRepository repository = new LocationRepository(this);
	
	// data
	private boolean isEdit = false;
	private Location location;
	
	// listeners
	private OnClickListener notesEditListener = new OnClickListener()
	{
		@Override
		public void onClick(View view)
		{
			Intent intent = new Intent(LocationEditActivity.this, NotesEditActivity.class);
			intent.putExtra(Constants.EXTRA_NOTES, location.getNotes());
			startActivityForResult(intent, REQUEST_NOTES);
		}
	};
	private OnClickListener deleteListener = new OnClickListener()
	{
		@Override
		public void onClick(View view)
		{
			showDeleteWarning();
		}
	};
	private DialogInterface.OnClickListener deleteConfirmListener = new DialogInterface.OnClickListener()
	{
		@Override
		public void onClick(DialogInterface arg0, int arg1)
		{
			delete();
		}
	};
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		
		// load data
		loadData();
		
		// init ui
		initializeUI();
	}
	
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent intent)
	{
		if (requestCode == REQUEST_NOTES && resultCode == RESULT_OK)
		{
			String notes = intent.getStringExtra(Constants.EXTRA_NOTES);
			updateNotes(notes);
		}
	}

	private void loadData()
	{
		// set isEdit flag
		Bundle extras = getIntent().getExtras();
		isEdit = (extras != null && extras.containsKey(Constants.EXTRA_LOCATION_ID));

		// determine add vs. edit
		if (isEdit)
		{
			// editing
			int locationId = getIntent().getExtras().getInt(Constants.EXTRA_LOCATION_ID);
			location = repository.getLocation(locationId);
		}
		else
		{
			// adding
			location = new Location();
		}
	}
	
	private void initializeUI()
	{
		// show view
		setContentView(R.layout.location_edit_view);
		
		// set header text
		TextView headerTextView = (TextView)findViewById(R.id.EditHeaderTextView);
		headerTextView.setText(isEdit ?
				R.string.location_edit_label : R.string.location_new_label);
		
		// set name
		EditText nField = (EditText)findViewById(R.id.LocationNameField);
		nField.setText(location.getName());
		
		// set home
		CheckBox hField = (CheckBox)findViewById(R.id.LocationHomeField);
		hField.setChecked(location.isHome());
		
		// set notes
		TextView notesField = (TextView)findViewById(R.id.LocationNotesField);
		notesField.setText(location.getNotes());
		notesField.setOnClickListener(notesEditListener);
		
		// add button listeners		
		Button deleteButton = (Button)findViewById(R.id.LocationDeleteButton);
		deleteButton.setOnClickListener(deleteListener);
		
		// show/hide delete button
		deleteButton.setVisibility(isEdit ? View.VISIBLE : View.GONE);
	}
	
	private void showDeleteWarning()
	{
		// create alert dialog
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setMessage(R.string.location_delete_warning);
		builder.setIcon(android.R.drawable.ic_dialog_alert);
		builder.setTitle(R.string.delete_confirmation);
		builder.setPositiveButton(R.string.yes_button, deleteConfirmListener);
		builder.setNegativeButton(R.string.no_button, null);
		AlertDialog dialog = builder.create();
		// show dialog
		dialog.show();
	}
	
	private void updateNotes(String notes)
	{
		// update location
		location.setNotes(notes);
		
		// update ui
		TextView notesField = (TextView)findViewById(R.id.LocationNotesField);
		notesField.setText(location.getNotes());
	}

	private void save()
	{
		// set name
		EditText nField = (EditText)findViewById(R.id.LocationNameField);
		location.setName(nField.getText().toString());
		
		// set is home
		CheckBox hField = (CheckBox)findViewById(R.id.LocationHomeField);
		location.setHome(hField.isChecked());
				
		// save
		if (isEdit)
		{
			repository.updateLocation(location);
		}
		else
		{
			repository.addLocation(location);
		}
		
		// finish
		finish();
	}
	
	private void delete()
	{
		// delete location
		repository.deleteLocation(location);
		
		// finish
		finish();
	}
	
	@Override
	public void onBackPressed()
	{
		save();
		super.onBackPressed();
	}
}
